﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Xml.Linq;
using System.Collections.Generic;

namespace WebApplication1
{
    public partial class client : System.Web.UI.Page
    {
        private Int32 clId;
        private Int32 agId;
        private Int32 cert;
        private Int32 i = 0;
        private String lab;
        private String role;
        private string name;
        private string surname;
        private int id;

        private string Сon = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        
        protected void Page_Load(object sender, EventArgs e)
        {

            id = Convert.ToInt32(Session["id"]);
            role = Convert.ToString(Session["role"]);
            name = Convert.ToString(Session["name"]);
            surname = Convert.ToString(Session["surname"]);
            Label2.Text = Convert.ToString(id);
            agId = Convert.ToInt32(Label2.Text);
            DropDownList1.Items.Add(Label2.Text);
            Session.Remove("id");
            Session.Remove("role");
            Session.Remove("name");
            Session.Remove("surname");
            Session["id"] = id;
            Session["name"] = name;
            Session["surname"] = surname;
            Session["role"] = role;
            Label4.Text = ("Здравствуйте, " + name + " " + surname + "!!!");
            Label5.Text = ("Вы зашли под правами " + role);
            DropDownList1.SelectedIndex = 0;
            Label2.Text = DropDownList1.SelectedValue;
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBoxCertificate.Text = "";
            TextBoxAddress.Text = "";
            TextBoxNumberRooms.Text = "";
            TextBoxSqery.Text = "";
            TextBoxYear.Text = "";
            TextBoxPriceFlat.Text = "";
            TextBoxPricePolis.Text = "";
            CheckBoxFloor.Checked = false;
            CheckBoxRobbery.Checked = false;
            CheckBoxFire.Checked = false;
            CheckBoxWater.Checked = false;
            CheckBoxProblem.Checked = false;
            CheckBoxWear.Checked = false;
            HyperLink1.Visible = false;
            Panel1.Visible = true;
            LabelFinish.Visible = false;
        }

        protected void ButtonNext1Click(object sender, EventArgs e)
        {
            cert = Convert.ToInt32(TextBoxCertificate.Text);
            Panel1.Visible = false;
            Panel2.Visible = true;
            SqlConnection connection = new SqlConnection(Сon);
             connection.Open();

            if (CheckBoxFloor.Checked == true)
            {
                CheckBoxWater.Visible = false;
            }
            try
            {
                clId = Convert.ToInt32(GridView1.SelectedDataKey.Value);

                SqlCommand сommand1 = new SqlCommand("insert into Flat (Certificate,IdClient,Adress,LostFloor,Rooms,Square,Year,Price)" +
                    "VALUES ('" + Convert.ToInt32(TextBoxCertificate.Text) + "','" + clId + "','" + TextBoxAddress.Text + "','" + CheckBoxFloor.Checked + "','" + Convert.ToInt32(TextBoxNumberRooms.Text) + "','" + Convert.ToDouble(TextBoxSqery.Text) + "','" + Convert.ToInt32(TextBoxYear.Text) + "','" + Convert.ToInt32(TextBoxPriceFlat.Text) + "')", connection);
                сommand1.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                LabelFinish.Text = ("Ошибка при вставке данных: " + ex.Message);
            }
            connection.Close();
        }

        protected void ButtonNextClick(object sender, EventArgs e)
        {  SqlConnection connection = new SqlConnection(Сon);
            try
            {
                agId = Convert.ToInt32(DropDownList1.SelectedValue);
              
                connection.Open();
                SqlCommand сommand1 = new SqlCommand("insert into Contract (IdFlat,IdAgent,Fire,Water,Wear,Problem,Loot,Date,Sum)" +
                    "VALUES ('" + Convert.ToInt32(TextBoxCertificate.Text) + "','" + agId + "','" + CheckBoxFire.Checked + "','" + CheckBoxWater.Checked + "','" + CheckBoxRobbery.Checked + "','" + CheckBoxProblem.Checked + "','" + CheckBoxWear.Checked + "','" + Convert.ToString(Calendar1.SelectedDate) + "','" + Convert.ToInt32(TextBoxPricePolis.Text) + "')", connection);
                сommand1.ExecuteNonQuery();
                connection.Close();
                Panel2.Visible = false;
                LabelFinish.Visible = true;
                LabelFinish.Text = "Новый договор успешно добавлен!";
                connection.Close();
            }
            catch (SqlException ex)
                connection.Close();
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);

            }
        }

        protected void ButtonPriceClick(object sender, EventArgs e)
        {
            Double fire = 0;
            Double water = 0;
            Double robbery = 0;
            Double polis = 0;

            SqlConnection connection = new SqlConnection(Сon);
            connection.Open();
            if (CheckBoxFire.Checked == true)
            {
                SqlCommand сommand1 = new SqlCommand("select fire from pricePolis", connection);
                SqlDataReader reader = сommand1.ExecuteReader();
                while (reader.Read())
                {
                    fire = Convert.ToDouble(reader["fire"]);
                }
                reader.Close();
            }
            else { fire = 0; }

            if (CheckBoxWater.Checked == true)
            {
                SqlCommand сommand2 = new SqlCommand("select water from pricePolis", connection);
                SqlDataReader reader2 = сommand2.ExecuteReader();
                while (reader2.Read())
                {
                    water = Convert.ToDouble(reader2["water"]);
                }
                reader2.Close();
            }
            else
            {
                water = 0;
            }
            if (CheckBoxRobbery.Checked == true)
            {
                SqlCommand сommand3 = new SqlCommand("select robbery from pricePolis", connection);
                SqlDataReader reader3 = сommand3.ExecuteReader();
                while (reader3.Read())
                {
                    robbery = Convert.ToDouble(reader3["robbery"]);
                }
                reader3.Close();
            }
            else
            {
                robbery = 0;
            }
            connection.Close();
            polis = ((Convert.ToDouble(TextBoxPriceFlat.Text))) * (water + fire + robbery);
            TextBoxPricePolis.Text = Convert.ToString(polis);
            
        }
    }
}
